<?php
namespace app\common\model\driver;

use think\facade\Request;
use app\common\model\BaseModel;

class Driver extends BaseModel
{
    protected $name = 'driver';
    protected $pk = 'driver_id';

    public function get_list($param)
    {
        $where[] = ['d.is_delete','=',0];

        if(!empty($param['reg_date'])){

            $param['reg_date'][0] = strtotime($param['reg_date'][0].' 00:00:00');
            $param['reg_date'][1] = strtotime($param['reg_date'][1].' 23:59:59');
            $where[] = ['d.create_time','between',$param['reg_date']];

        }
        if (!empty($param['status'])) {
            if($param['status'] != ''){
                $where[] = ['d.status','=',$param['status']];
            }

        }
        if (!empty($param['realname'])) {
            $where[] = ['d.realname','=',$param['realname']];
        }
        if (!empty($param['selectType'])) {
            $where[] = ['d.driver_type','=',$param['selectType']];
        }
        if (!empty($param['selectYears'])) {
            $where[] = ['d.driver_age','=',$param['selectYears']];

        }
        if (!empty($param['selectSex'])) {
            $where[] = ['d.sex','=',$param['selectSex']];
        }
        return $this->alias('d')
            ->field('d.*,da.driver_age as age_name,dt.type_name')
            ->join("jjjshop_user u","d.user_id=u.user_id","left")
            ->join('jjjshop_driver_age da','d.driver_age=da.id','left')
            ->join('jjjshop_driver_type dt','d.driver_type=dt.type_id','left')
            ->where($where)->order('d.create_time desc')->paginate($param,false);
    }

    public function shop_list($param)
    {
        $model = new static();
        $where[] = ['d.is_delete','=',0];
        $where[] = ['u.is_delete','=',0];
        $where[] = ['d.status','=',1];
        if (!empty($param['selectType'])) {
            $where[] = ['d.driver_type','=',$param['selectType']];

        }
        if (!empty($param['selectYears'])) {
            $where[] = ['d.driver_age','=',$param['selectYears']];

        }
        if (!empty($param['selectSex'])) {
            $where[] = ['d.sex','=',$param['selectSex']];
        }
        return $this->alias('d')
            ->field('d.*,da.driver_age as age_name,dt.type_name')
            ->join("jjjshop_user u","d.user_id=u.user_id","left")
            ->join('jjjshop_driver_age da','d.driver_age=da.id','left')
            ->join('jjjshop_driver_type dt','d.driver_type=dt.type_id','left')
            ->where($where)->order('create_time desc')->paginate($param,false);
    }

    public function add($data,$user)
    {
        unset($data['token']);
        unset($data['type_name']);
        unset($data['age_name']);
        unset($data['sexS']);
        $check = $this->where(array('user_id'=>$user['user_id']))->find();
        if($check){
            $data['status'] = 0;
            $res = $this->where(array('driver_id'=>$check['driver_id']))->update($data);
        }else{
            $data['app_id'] = self::$app_id;
            $res = $this->save($data);
        }

        return $res;
    }

    public function detail($id)
    {
        return $this->where(array('driver_id'=>$id))->find();
    }
}